/*
 * @Date: 2024-05-29 11:28:36
 * @LastEditors: 陈俊宇 467724386@qq.com
 * @LastEditTime: 2024-08-13 18:49:24
 */
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import ElementPlus from 'unplugin-element-plus/vite'
import postCssPxToRem from 'postcss-pxtorem'

// 定义项目根目录
const root = resolve(__dirname, '.')

export default defineConfig({
    plugins: [
        vue(),
        AutoImport({
            resolvers: [ElementPlusResolver()],
        }),
        Components({
            resolvers: [ElementPlusResolver()],
            directoryAsNamespace: true,
        }),
        ElementPlus({
            useSource: true,
        }),
    ],
    base: './',
    server: {
        host: '0.0.0.0',
    },
    build: {
        lib: {
            entry: resolve(root, 'src/index.js'),
            name: 'ServiceChat',
        },
        rollupOptions: {
            // 使外部依赖成为全局变量
            external: ['vue'],
            output: {
                entryFileNames: 'service-chat.js',
                // chunkFileNames: 'assets/[name].[hash].js',
                assetFileNames: 'service-chat.css',
                format: 'umd',
                name: 'ServiceChat',
                globals: {
                    vue: 'Vue',
                },
            },
        },
    },
    resolve: {
        alias: {
            '@': resolve(root, 'src'),
        },
    },
    css: {
        // postcss: {
        //     plugins: [
        //         postCssPxToRem({
        //             rootValue: 100, // 1rem，根据 设计稿宽度/10 进行设置
        //             propList: ['*'], // 需要转换的属性，这里选择全部都进行转换
        //         }),
        //     ],
        // },
        extract: true,
        preprocessorOptions: {
            scss: {
                additionalData: `@use "@/styles/element/index.scss" as *;@use "@/styles/animate.scss";`,
            },
        },
    },
    // 添加 define 选项来模拟 process 环境
    define: {
        global: {},
        'process.env': {},
    },
})
